package cn.hrlly.tao2shou.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.hrlly.tao2shou.R;
import cn.hrlly.tao2shou.other.Config;
import cn.hrlly.tao2shou.presenter.RegisterPresenter;
import cn.hrlly.tao2shou.presenter.RegisterPresenterImpl;
import cn.hrlly.tao2shou.view.base.BaseActivity;
import cn.hrlly.tao2shou.view.view_interface.RegisterView;

public class RegisterActivity extends BaseActivity implements RegisterView {

    @Bind(R.id.etPhoneNumber)
    EditText etPhoneNumber;
    @Bind(R.id.etPassword)
    EditText etPassword;
    @Bind(R.id.etCode)
    EditText etCode;
    @Bind(R.id.btnGetCode)
    Button btnGetCode;
    @Bind(R.id.btnRegister)
    Button btnRegister;
    private RegisterPresenter registerPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        //控件绑定
        ButterKnife.bind(this);
        //初始化registerPresenter
        registerPresenter = new RegisterPresenterImpl(this, this);
    }

    //btnGetCode点击事件
    @OnClick(R.id.btnGetCode)
    public void getCode() {
        if (!etPhoneNumber.getText().toString().equals("")) {
            if (etPhoneNumber.getText().toString().length() == 11)
                registerPresenter.getCode();
            else
                showMessage(R.string.phone_number_is_error);
        } else {
            showMessage(R.string.phone_number_can_not_be_empty);
        }
    }

    //btnRegister点击事件
    @OnClick(R.id.btnRegister)
    public void register() {
        if (!etPhoneNumber.getText().toString().equals("") && !etCode.getText().toString().equals("") && !etPassword.getText().toString().equals("")) {
            if(etPhoneNumber.getText().toString().length()==11)
                registerPresenter.judgeCode();
            else
                showMessage(R.string.phone_number_is_error);
        } else {
            showMessage(R.string.fill_incomplete);
        }
    }

    //以下是对activity的一些操作
    @Override
    public String getPhoneNumber() {
        return etPhoneNumber.getText().toString();
    }

    @Override
    public String getVerificationCode() {
        return etCode.getText().toString();
    }

    @Override
    public void startActivity() {
        Intent intent = new Intent(RegisterActivity.this, CollectUserInfoActivity.class);
        intent.putExtra(Config.key_phone_number, etPhoneNumber.getText().toString());
        intent.putExtra(Config.key_password, etPassword.getText().toString());
        intent.putExtra(Config.key_verifivation_code, etCode.getText().toString());
        startActivity(intent);
        finish();
    }

    @Override
    public void showMessage(String errorMessage) {
        Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showMessage(int errorMessage) {
        Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT).show();
    }
}
